##------------------------------------------------------------------------------
##
##	yasco-1.0 - Yet Another (Java)Script Obfuscator
##	Copyright (C) 2008-2011 Oliver Schieche <post@oliver-schieche.de>
##
##	Makes use of Yapp
##	Parse::Yapp - Copyright (c) 1998-2001, Francois Desarmenien, all right reserved.
##
##	$Id: HashEncrypt.pm 8 2011-07-07 08:42:03Z goresplatter1844@gmail.com $
##------------------------------------------------------------------------------
package Yasco::Filter::HashEncrypt;

use strict;
use vars qw[@ISA];

use Data::Dumper;
use Yasco::Filter::Result;
##------------------------------------------------------------------------------
BEGIN {
	require Yasco::Filter;
	@ISA = qw[Yasco::Filter];
}
##------------------------------------------------------------------------------
sub tokens
	{
		'HASH'
	}
##------------------------------------------------------------------------------
sub new
	{
	    my $self  = shift;
	    my $options = shift;
	    $self = $self->SUPER::new($options);
	    $self->{level} ||= 1;

	    $self
	}
##------------------------------------------------------------------------------
sub execute
	{
		my $self = shift;
		my ($o,$node) = @_;
		my ($id,$code);

		$id = sprintf('h%x', int(99*rand));
		$code = '(function(){var ' .$id. '={};';

		foreach my $kv (@{$node->left || []})
			{
				die __FILE__.':'.__LINE__.':'.Dumper($kv) if 'KV' ne $kv->op;

				$code .= $id . '[';

				KV_LEFT: for ($kv->left->op) {
					'ID' eq $_ and do {
						$code .= $o->map_string("'" . $kv->left->left . "'");
						last KV_LEFT;
					};

					'NUMBER' eq $_ and do {
						$code .= $kv->left->reconstruct;
						last KV_LEFT;
					};

					'STRING' eq $_ and do {
						$code .= $o->map_string($kv->left->left);
						last KV_LEFT;
					};

					die __FILE__.':'.__LINE__.':KV\'s left member is odd:'.Dumper($kv);
				}

				$code .= ']=' . $kv->right->reconstruct . ';';
				$code .= "\n" if (1 & $o->option(qq{newlines}));
			}

		$code .= "return $id;})()";
		throw Yasco::Filter::Result($code)
	}
##------------------------------------------------------------------------------
sub post_process
	{
	}
##------------------------------------------------------------------------------
1